package org.kust.bbs.admin.controller;

import org.kust.bbs.admin.service.IUserService;
import org.kust.bbs.common.web.JsonPage;
import org.kust.bbs.common.web.JsonResult;
import org.kust.bbs.pojo.dto.UserAddNewDTO;
import org.kust.bbs.pojo.vo.PageQuery;
import org.kust.bbs.pojo.vo.UserDetailsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping(value = "/user", produces = "application/json; charset=utf-8")
public class UserController {
    @Autowired
    private IUserService userService;

    @PostMapping("add-new")
    public JsonResult<Void> addNew(UserAddNewDTO userAddNewDTO) {
        userService.addNew(userAddNewDTO);
        return JsonResult.ok();
    }

    @GetMapping("update-enable")
    public JsonResult<Void> updateEnableByUsername(String username) {
        userService.updateEnableByUsername(username);
        return JsonResult.ok();
    }

    @GetMapping("list")
    public JsonResult<JsonPage<UserDetailsVO>> list(PageQuery pageQuery) {
        JsonPage jsonPage = userService.list(pageQuery);
        return JsonResult.ok(jsonPage);
    }

    @GetMapping("ip")
    public JsonResult<Map<String, Integer>> ip() {
        Map<String, Integer> map = userService.ip();
        return JsonResult.ok(map);
    }
}
